package com.ems.dao;

import com.ems.model.Salary;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SalaryDAO {
    // 添加工资记录
    public void addSalary(Salary salary) throws SQLException {
        String sql = "INSERT INTO salaries (employee_id, base_salary, bonus, deduction, payment_date, status) " +
                "VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, salary.getEmployeeId());
            stmt.setDouble(2, salary.getBaseSalary());
            stmt.setDouble(3, salary.getBonus());
            stmt.setDouble(4, salary.getDeduction());
            stmt.setDate(5, new java.sql.Date(salary.getPaymentDate().getTime()));
            stmt.setString(6, salary.getStatus());
            stmt.executeUpdate();
        }
    }

    // 删除工资记录
    public void deleteSalary(int id) throws SQLException {
        String sql = "DELETE FROM salaries WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            stmt.executeUpdate();
        }
    }

    // 更新工资记录
    public void updateSalary(Salary salary) throws SQLException {
        String sql = "UPDATE salaries SET employee_id=?, base_salary=?, bonus=?, deduction=?, " +
                "payment_date=?, status=? WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, salary.getEmployeeId());
            stmt.setDouble(2, salary.getBaseSalary());
            stmt.setDouble(3, salary.getBonus());
            stmt.setDouble(4, salary.getDeduction());
            stmt.setDate(5, new java.sql.Date(salary.getPaymentDate().getTime()));
            stmt.setString(6, salary.getStatus());
            stmt.setInt(7, salary.getId());
            stmt.executeUpdate();
        }
    }

    // 获取所有工资记录
    public List<Salary> getAllSalaries() throws SQLException {
        List<Salary> salaries = new ArrayList<>();
        String sql = "SELECT * FROM salaries";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Salary salary = new Salary();
                salary.setId(rs.getInt("id"));
                salary.setEmployeeId(rs.getInt("employee_id"));
                salary.setBaseSalary(rs.getDouble("base_salary"));
                salary.setBonus(rs.getDouble("bonus"));
                salary.setDeduction(rs.getDouble("deduction"));
                salary.setPaymentDate(rs.getDate("payment_date"));
                salary.setStatus(rs.getString("status"));
                salaries.add(salary);
            }
        }
        return salaries;
    }

    // 根据ID获取工资记录
    public Salary getSalaryById(int id) throws SQLException {
        String sql = "SELECT * FROM salaries WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    Salary salary = new Salary();
                    salary.setId(rs.getInt("id"));
                    salary.setEmployeeId(rs.getInt("employee_id"));
                    salary.setBaseSalary(rs.getDouble("base_salary"));
                    salary.setBonus(rs.getDouble("bonus"));
                    salary.setDeduction(rs.getDouble("deduction"));
                    salary.setPaymentDate(rs.getDate("payment_date"));
                    salary.setStatus(rs.getString("status"));
                    return salary;
                }
            }
        }
        return null;
    }

    // 根据员工ID获取工资记录
    public List<Salary> getSalariesByEmployee(int employeeId) throws SQLException {
        List<Salary> salaries = new ArrayList<>();
        String sql = "SELECT * FROM salaries WHERE employee_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, employeeId);
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Salary salary = new Salary();
                    salary.setId(rs.getInt("id"));
                    salary.setEmployeeId(rs.getInt("employee_id"));
                    salary.setBaseSalary(rs.getDouble("base_salary"));
                    salary.setBonus(rs.getDouble("bonus"));
                    salary.setDeduction(rs.getDouble("deduction"));
                    salary.setPaymentDate(rs.getDate("payment_date"));
                    salary.setStatus(rs.getString("status"));
                    salaries.add(salary);
                }
            }
        }
        return salaries;
    }
}